﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.BLL;
using STLGROUP.Layers.Common;
using STLGROUP.Layers.DataAccessLayers;

namespace QuanLyBanHang
{
    /// <summary>
    /// MF007 - Don hang su dung
    /// </summary>
    /// <author>viet sang</author>
    public partial class MF007 : frmManager
    {
        DT_HoaDonBLL _business;
        ToolStripButton _btnThanhToan;

        public MF007()
        {
            InitializeComponent();
            pnlChiTiet.Enabled = true;
            _business = new DT_HoaDonBLL();

            tsbBaoCao.Text = "In đơn hàng";
            tsbBaoCao.Click += new EventHandler(tsbBaoCao_Click);
            //_bindingSource.CurrentChanged += new EventHandler(_bindingSource_CurrentChanged);

            //add controls
            this._btnThanhToan = new ToolStripButton();
            this._btnThanhToan.AutoSize = true;
            this._btnThanhToan.ForeColor = System.Drawing.Color.Black;
            this._btnThanhToan.ImageTransparentColor = System.Drawing.Color.Magenta;
            this._btnThanhToan.Name = "btnThanhToan";
            this._btnThanhToan.Padding = new System.Windows.Forms.Padding(3, 0, 4, 0);
            this._btnThanhToan.Text = "Thanh toán công nợ";
            this._btnThanhToan.Visible = true;           
            
            _btnThanhToan.Click += new EventHandler(_btnThanhToan_Click);
                        
            toolStrip1.Items.Insert(12, _btnThanhToan);
        }

        private void _btnThanhToan_Click(object sender, EventArgs e)
        {
            DonHangSuDung donHang = this._bindingSource.Current as DonHangSuDung;
            if (donHang != null&& donHang.ConLai.HasValue && donHang.ConLai.Value>0)
            {
                PF004 frm = new PF004() { IdDonHang = donHang.IDDonHang, Mode = 1 };
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Filter();
                    //Cache.Context.SaveChanges();
                }
            }
        }

        private void tsbBaoCao_Click(object sender, EventArgs e)
        {
            if (_bindingSource.Current != null)
            {
                DonHangSuDung hoaDon = _bindingSource.Current as DonHangSuDung;
                if (hoaDon != null)
                {
                    RF004 frmReport = new RF004() { IDDonHang = hoaDon.IDDonHang };
                    frmReport.ShowDialog();
                }
            }
            else
            {
                UICommon.ShowMessgeString("Chưa chọn đơn hàng", "Thông báo");
            }
        }

        protected override void LoadData()
        {
            txtTuNgay.Value = DateTime.Today.Subtract(new TimeSpan(7, 0, 0, 0));
            txtDenNgay.Value = DateTime.Today.AddDays(1);
            base.LoadData();
        }

        protected override void Filter()
        {
            var data = (from hd in Cache.Context.DT_DonDatHang
                       join cthd in Cache.Context.DT_ChiTietDonHang on hd.IDDonDatHang equals cthd.IDDonDatHang
                       join kh in Cache.Context.DT_NhaCungCap on hd.IDKhachHang equals kh.IDNhaCungCap
                       join nv in Cache.Context.DT_NhanVien on hd.IDNhanVienXacNhan equals nv.IDNhanVien
                        where (hd.NgayTao.HasValue || txtTuNgay.Value <= hd.NgayTao)
                        && (hd.NgayTao.HasValue || txtDenNgay.Value >= hd.NgayTao)
                       select new { 
                           hd.IDDonDatHang,
                           hd.IDLoaiGiaoDich,
                           hd.MaDonDatHang,
                           hd.TongTien,
                           hd.DaThanhToan,
                           ConLai = hd.TongTien - hd.DaThanhToan,
                           nv.MaNhanVien,
                           HoTenNhanVien = nv.HoVaTenDem+" "+nv.TenNhanVien,
                           kh.MaNhaCungCap,
                           kh.TenNhaCungCap,
                           hd.NgayTao
                       }).Distinct();
            List<DonHangSuDung> dataSource = data.ToList().ConvertAll<DonHangSuDung>(s => new DonHangSuDung
            { 
                IDDonHang = s.IDDonDatHang,
                IDLoaiDonHang=s.IDLoaiGiaoDich,
                MaDonHang=s.MaDonDatHang,
                TongTien=s.TongTien,
                DaTra = s.DaThanhToan,
                ConLai= s.ConLai,
                MaNhanVien =s.MaNhanVien,
                HoTenNhanVien=s.HoTenNhanVien,
                MaNhaCungCap=s.MaNhaCungCap,
                TenNhaCungCap=s.TenNhaCungCap,
                NgayTao = s.NgayTao.HasValue? s.NgayTao.Value.ToString("dd/MM/yyyy"):string.Empty
            }).ToList();

            _bindingSource.DataSource = dataSource;
            //_business.GetHoaDonDaSuDung(txtTuNgay.Value, txtDenNgay.Value);
            this.stlDataGridView1.DataSource = _bindingSource;
        }

        public class DonHangSuDung
        {
            public int IDDonHang { get; set; }
            public int? IDLoaiDonHang { get; set; }
            public string MaDonHang { get; set; }
            public decimal? TongTien { get; set; }
            public decimal? DaTra { get; set; }
            public decimal? ConLai { get; set; }
            public string MaNhanVien { get; set; }
            public string HoTenNhanVien { get; set; }
            public string MaNhaCungCap { get; set; }
            public string TenNhaCungCap { get; set; }
            public string NgayTao { get; set; }

        }
    }
}
